package com.example.mc.listener;

import com.example.common.base.constant.MqQueueConstant;
import com.example.common.msg.dto.MemberLogDTO;
import com.example.common.msg.template.MsgTemplate;
import com.example.mc.handler.MessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class MessageListener {

    @Autowired
    private Map<String, MessageHandler> messageHandlerMap;

    @RabbitListener(queues = MqQueueConstant.MEMBER_LOG)
    public void receive(MsgTemplate<MemberLogDTO> msgTemplate){
        long startTime = System.currentTimeMillis();
        MemberLogDTO memberLog = msgTemplate.getData();
        log.info("消息中心接收到会员日志请求-> 用户ID：{} -> 日志标题: {}", memberLog.getCreateBy(), memberLog.getTitle());
        String type = msgTemplate.getType();
        MessageHandler messageHandler = messageHandlerMap.get(type);
        messageHandler.execute(msgTemplate);
        long useTime = System.currentTimeMillis() - startTime;
        log.info("调用 {} 消息处理完毕，耗时 {}毫秒", type, useTime);
    }

}
